package com.mobile.framework.tag;

import com.mobile.framework.util.HtmlUtil;
import org.apache.struts.taglib.TagUtils;
import org.xml.sax.SAXException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
import java.io.IOException;
import java.lang.reflect.Field;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * Created by IntelliJ IDEA.
 * User: bj
 * Date: 2010-7-9
 * Time: 10:23:58
 * To change this template use File | Settings | File Templates.
 */

/*
out_test.jsp
<%@ page import="java.util.Date" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"  %>
<%@ taglib uri="/WEB-INF/app.tld" prefix="app" %>
<%@ page isELIgnored="false" %>
<%
    Date date = new Date();
    request.setAttribute("date",date);
%>
<app:out value="${date}" pattern="yyyy-MM-dd MMM EEE" type="date" />
<br/>
<app:out value="${date}" pattern="yyyy-MM-dd MMM EEE" type="date" locale="ENGLISH" />
<br/>
<app:out value="${123456789.123456789}" pattern="#,##0.00" type="number" />
<br/>
<app:out value="${0}" pattern="#,##0.00" type="number" zeroExc="false" />
<br/>
<app:out value="${0}" pattern="#,##0.00" type="number" />
<br/>
<app:out value="一2三4五6七9九0" length="5" />
<br/>
<app:out value="一2三4五6七9九0" fontLength="5" />

*/
public class OutTag extends TagSupport {

    String type;
    String value;
    Integer length;
    Boolean zeroExc;
    String pattern;
    Boolean xmlParse;
    Boolean trim;
    Integer fontLength;
    String tail;
    String locale;
    Boolean encodeURIComponent;

    public Boolean getEncodeURIComponent() {
        return encodeURIComponent;
    }

    public void setEncodeURIComponent(Boolean encodeURIComponent) {
        this.encodeURIComponent = encodeURIComponent;
    }

    public String getLocale() {
        return locale;
    }

    public void setLocale(String locale) {
        this.locale = locale;
    }

    public String getTail() {
        return tail;
    }

    public void setTail(String tail) {
        this.tail = tail;
    }

    public Boolean isTrim() {
        return trim;
    }

    public void setTrim(boolean trim) {
        this.trim = trim;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public int getLength() {
        return length;
    }

    public void setLength(int length) {
        this.length = length;
    }

    public Boolean isZeroExc() {
        return zeroExc;
    }

    public void setZeroExc(boolean zeroExc) {
        this.zeroExc = zeroExc;
    }

    public String getPattern() {
        return pattern;
    }

    public void setPattern(String pattern) {
        this.pattern = pattern;
    }

    public Boolean isXmlParse() {
        return xmlParse;
    }

    public void setXmlParse(boolean xmlParse) {
        this.xmlParse = xmlParse;
    }

    public int getFontLength() {
        return fontLength;
    }

    public void setFontLength(int fontLength) {
        this.fontLength = fontLength;
    }

    public OutTag(){

    }

    public Locale getTagLocale() {
        if (this.locale != null) {
            try {
                Field[] Fields = Locale.class.getDeclaredFields();
                Locale locale = Locale.getDefault();
                for (int i = 0; i < Fields.length; i++) {
                    if ( this.locale.toLowerCase().equals(Fields[i].getName().toLowerCase())) {
                        return (Locale) Fields[i].get(this.locale);
                    }
                }
            } catch (Exception e) {
                //e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
        }
        return Locale.CHINESE;
    }


    public int doStartTag() throws JspException {


        String outValue = this.value;
        if (pattern != null) {
            if (type == null || type.toLowerCase().equals("string")) {

            } else if (type.toLowerCase().equals("number")) {
                if (outValue.equals("")) {
                    outValue = "0";
                }
                Double dValue = Double.valueOf(outValue);
                if ( (zeroExc == null || new Boolean(true).equals(zeroExc))  && dValue.intValue() == 0 ) {
                    outValue = "0";
                } else {
                    DecimalFormat decimalFormat = new DecimalFormat(pattern);
                    outValue = decimalFormat.format(dValue);
                }
            } else if (type.toLowerCase().equals("date")) {
                if (outValue.equals("")) {
                    outValue = "";
                } else {
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH);
                    try {
                        //System.out.println(outValue);
                        Date dValue = simpleDateFormat.parse(outValue);
                        simpleDateFormat = new SimpleDateFormat(pattern, getTagLocale());
                        outValue = simpleDateFormat.format(dValue);
                    } catch (ParseException e) {
                        //e.printStackTrace();
                        //To change body of catch statement use File | Settings | File Templates.
                    }
                }
            }
        }

        if ( new Boolean(true).equals(xmlParse) ) {
            try {
                outValue = HtmlUtil.removeHtmlCode(outValue);
            } catch (SAXException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            } catch (IOException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
        }

        if ( new Boolean(true).equals(trim) ) {
            outValue = outValue.trim();
        }

        boolean ifTail = false;

        if (fontLength != null) {
            int tFontLength = 0;
            StringBuilder StringBuilder = new StringBuilder();
            for (int i = 0; i < fontLength; i++) {
                String t = outValue.substring(i, i + 1);
                int length = t.getBytes().length;
                tFontLength = tFontLength + length;
                StringBuilder.append(t);
                if (tFontLength >= fontLength) {
                    break;
                }
            }
            if ( outValue.length() > StringBuilder.toString().length() ) {
                ifTail = true;
            }
            outValue = StringBuilder.toString();
        }


        if (length != null) {
            if (outValue.length() > length) {
                outValue = outValue.substring(0, length);
                ifTail = true;
            }
        }

        if (ifTail) {
            outValue += this.tail == null ?  "..." : this.tail ;
        }

        if(new Boolean(true).equals(encodeURIComponent)){
            outValue = outValue == null ? "" : HtmlUtil.encodeURIComponent(outValue);
        }

        TagUtils.getInstance().write(pageContext, outValue);

        return (SKIP_BODY);
    }


}
